<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>订单</title>
	<meta name="decorator" content="default"/>
	<style>.input-number{width:30px;}</style>
	<script type="text/javascript">
		$(document).ready(function() {
			$("#purchasenum").focus();
			$('input').keyup(countTotalPrice);
			$("#inputForm").validate({
				rules: {
					purchasenum: {digits:true,child:true,totalnum:true}
				},
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		
		// 票数总和不能为空
		$.validator.addMethod("purchase", function(value, element) {
			var purchasenum = $('#purchasenum').val();
			if(!isNaN(purchasenum) && purchasenum>0){
				return true;
			} else {
				return false;
			}
		}, "购票数量不能为空");
		var leftSeats = ${realsche.leftSeats};
		$.validator.addMethod("totalnum", function(value, element) {
			var purchasenum = $('#purchasenum').val();
			var total=0;
			if(!isNaN(purchasenum) && purchasenum > 0){
				total+=purchasenum;
			}
			if(total > leftSeats){
				return false;
			}
			return true;
		}, "购票数量超过剩余票数");
		
		// 计算总价格
		function countTotalPrice(){
			var totalPrice = 0;
			var purchasenum = $('#purchasenum').val();
			if(!isNaN(purchasenum) && purchasenum > 0){
				totalPrice+=purchasenum*${realsche.retailprice};
			}
			$('#totalPrice').val("￥"+totalPrice); 
		}
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active">订单信息</li>
	</ul>
	<form:form id="inputForm" modelAttribute="realsche"
		action="${ctx}/realsche/booking" method="post" class="form-horizontal">
		<form:hidden path="realscheid" />
		<form:hidden path="realboardid" />
		<form:hidden path="realoffid" />
		<form:hidden path="departdateStr" />
		<sys:message content="${message}" />
		<div class="control-group">
			<label class="control-label">班次名称</label>
			<div class="controls">
				<input type="text" value="${realsche.schename}" readonly/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">发车日期</label>
			<div class="controls">
				<input type="text" value="${realsche.departdateStr}&nbsp;${realsche.boardtime}" readonly/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">上客地点</label>
			<div class="controls">
				<input type="text" value="${realsche.boardaddr}" readonly/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">下客地点</label>
			<div class="controls">
				<input type="text" value="${realsche.offaddr}" readonly/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">剩余票数（张）</label>
			<div class="controls">
				<input type="text" value="${realsche.leftSeats}" readonly/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">票价（元）</label>
			<div class="controls">
				<input type="text" value="￥${realsche.retailprice}" readonly/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="purchasenum">购票数量（张）</label>
			<div class="controls">
				<input type="text" id="purchasenum" name="purchasenum" class="input-number">
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">累计价格（元）</label>
			<div class="controls">
				<input id="totalPrice" type="text" value="" readonly/>
			</div>
		</div>
		<div class="form-actions">
			<input class="btn btn-primary" type="submit" value="提交订单"/>
			<input class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>